package com.applancer.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.applancer.nativecmsflip.R;
import com.applancer.nativecmsflip.UserActivity;
import com.applancer.provider.ContentManager;

public class ProfileFragment extends Fragment {

	protected View contentView;
	protected TextView user_name;
	protected TextView email;
	protected TextView url;
	protected ContentManager manager;
	protected Button logout;

	public static final String MYPREFERENCES = "LoginRef";
	public static final String LOG_STATE = "login_state";

	public SharedPreferences sharedpreferences;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentView = inflater.inflate(R.layout.profile_fragment, null);
		return contentView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initial();
		execution();
	}

	private void initial() {
		sharedpreferences = contentView.getContext().getSharedPreferences(
				MYPREFERENCES, Context.MODE_PRIVATE);
		user_name = (TextView) contentView.findViewById(R.id.profile_user_name);
		email = (TextView) contentView.findViewById(R.id.profile_email);
		url = (TextView) contentView.findViewById(R.id.profile_url);

		manager = ContentManager.getInstance();
		logout = (Button) contentView.findViewById(R.id.logout);
	}

	private void execution() {
		user_name.setText(manager.currentUser.getUserName());
		email.setText(manager.currentUser.getEmail());
		url.setText(manager.currentUser.getUrl());
		logout.setOnClickListener(logoutevent);
	}

	OnClickListener logoutevent = new OnClickListener() {

		@Override
		public void onClick(View v) {
			((UserActivity) contentView.getContext()).change2login();
			Editor editor = sharedpreferences.edit();
			editor.putBoolean(LOG_STATE, false);
			editor.commit();
		}
	};
}
